package srcpackage;





import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


/**
 * @version 24/3/2011 20.42
 * @author Pieter & An
 *
 */
public class SquareTest {

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		newSquare = new Square(200, new BigDecimal ("0.200"));
		mergeSquare =  new Square(200, new BigDecimal ("0.60"));
		
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	public Square newSquare;
	public Square mergeSquare;
	
	
	@Test
	public final void firstConstructorLegal(){
		Square otherSquare = new Square(50,new BigDecimal ("0.20"));
		assertEquals(50, otherSquare.getTemperature(),0);
	}
	
	@Test	(expected = IllegalTemperatureException.class)
	public final void firstConstructorIllegalTemperature(){
		Square otherSquare = new Square(6000000, new BigDecimal("0.50"));
	}
	
	@Test 
	public final void secondConstructor(){
		Square otherSquare = new Square();
		assertEquals(0, otherSquare.getTemperature(),0);
		assertEquals(new BigDecimal("0.50"),otherSquare.getHumidity());	
	}
	
	@Test (expected = IllegalTemperatureException.class)
	public final void setTemperatureMinus400(){
		newSquare.setTemperature(-400);	
	}
	
	@Test (expected = IllegalTemperatureException.class)
	public final void setTemperature400000(){
		newSquare.setTemperature(400000);	
	}	
	
	@Test
	public final void getTempKelvin(){
		assertEquals(473, newSquare.getKelvin(),0);	
	}
	
	@Test
	public final void getTempFahrenheit(){
		assertEquals(417.6, newSquare.getFahrenheit(),0);
	}
			
	@Test 
	public final void setTemperature200(){
		newSquare.setTemperature(200);
		assertEquals(200, newSquare.getTemperature(),0);		
	}
	
	@Test
	public final void setTemperatureLegalCase(){
		newSquare.setTemperature(20);
		assertEquals(20, newSquare.getTemperature(),0);
	}
	
	@Test	(expected = IllegalTemperatureException.class)
	public final void setTemperatureIllegalCase(){
		newSquare.setTemperature(60000);
	}
	
	@Test
	public final void isValidTemperatureTrueCase(){
		assertTrue(newSquare.isValidTemperature(100));
	}
	
	@Test
	public final void isValidTemperatureFalseCase6000(){
		assertFalse(newSquare.isValidTemperature(6000));
	}
	
	@Test
	public final void isValidTemperatureFalseCaseMinus6000(){
		assertFalse(newSquare.isValidTemperature(-6000));
	}
	
	@Test
	public final void isValidHumidityLegalcase(){
		assertTrue(newSquare.isValidHumidity(new BigDecimal("0.70")));
	}
	
	@Test
	public final void isValidHumidityIllegalcase(){
		assertFalse(newSquare.isValidHumidity(new BigDecimal("150.0")));
	}
	
	@Test
	public final void isValidHumiditySecondIllegalcase(){
		assertFalse(newSquare.isValidHumidity(new BigDecimal("-150.0")));
	}	
	
	@Test
	public final void isValidMinMaxTemperatureTrueCase(){
		assertTrue(newSquare.isValidMinMaxTemperature(-200.0, 100.0));
	}
	
	@Test
	public final void isValidMinMaxTemperatureTrueCaseEqualMinAndMax(){
		assertTrue(newSquare.isValidMinMaxTemperature(-200.0, 200.0));
	}
	
	@Test
	public final void isValidMinMaxTemperatureFalseCase(){
		assertFalse(newSquare.isValidMinMaxTemperature(200.0, -200.0));
	}	
	
	@Test
	public final void setMinMaxTemperatureTrueCase() throws Exception{
		newSquare.setMinMaxTemperature(-200.0, 100.0);
		assertEquals(-200, newSquare.getMIN_TEMPERATURE(), 0);
		assertEquals(100.0, newSquare.getMAX_TEMPERATURE(),0);
	}
	
	@Test
	public final void setMinMaxTemperatureTrueCaseEqualT() throws Exception{
			newSquare.setMinMaxTemperature(-200.0, -200.0);
			assertEquals(-200, newSquare.getMIN_TEMPERATURE(), 0);
			assertEquals(-200, newSquare.getMAX_TEMPERATURE(),0);
	}
	
	@Test (expected = IllegalMinMaxTemperatureException.class)
	public final void setMinMaxTemperatureFalseCase() throws Exception{
			newSquare.setMinMaxTemperature(-200.0, -300.0);		
	}
	
	@Test
	public final void ColdDamageValueBorderCase(){
		newSquare.setTemperature(-14);
		assertEquals(0, newSquare.coldDamage(), 0);	
	}
	
	@Test
	public final void ColdDamageforTMINUS200(){
		newSquare.setTemperature(-200);
		assertEquals(19, newSquare.coldDamage(), 0);	
	}
	
	@Test
	public final void ColdDamageforPlus200(){
		newSquare.setTemperature(200);
		assertEquals(0, newSquare.coldDamage(), 0);
	}
	
	@Test
	public final void HeatDamageValueBorderCase(){
		newSquare.setTemperature(49);
		assertEquals(0, newSquare.heatDamage(), 0);	
	}
	
	@Test
	public final void HeatDamageforTMINUS200(){
		newSquare.setTemperature(-200);
		assertEquals(0, newSquare.heatDamage(), 0);	
	}
	
	@Test
	public final void HeatDamageforPlus200(){
		newSquare.setTemperature(200);
		newSquare.setHumidity(new BigDecimal("0.10"));
		assertEquals(11, newSquare.heatDamage(), 0);
	}
	
	@Test
	public final void RustDamagefor10perc(){
		newSquare.setHumidity(new BigDecimal("0.1"));
		assertEquals(0, newSquare.rustDamage(), 0);
	}
	
	@Test
	public final void RustDamagefor50perc(){
		newSquare.setHumidity(new BigDecimal("0.5"));
		assertEquals(2, newSquare.rustDamage(), 0);
	}
	
	@Test
	public final void RustDamagefor36perc(){
		newSquare.setHumidity(new BigDecimal("0.36"));
		assertEquals(0, newSquare.rustDamage(), 0);	
	}

	@Test 	(expected =IllegalTemperatureException.class)
	public final void setHeatDamageParametersIllegalTemperatureForLimit(){
		newSquare.setHeatDamageParameters(-3000,50 );
	}
	
	@Test	(expected = IllegalArgumentException.class)
	public final void setHeatDamageParametersIllegalTemeperatureForAmount(){
		newSquare.setHeatDamageParameters(200, -10);
	}
	
	@Test	(expected = IllegalArgumentException.class)
	public final void setHeatDamageParametersIllegalTemeperatureForAmountTooBig(){
		newSquare.setHeatDamageParameters(200, 6000);
	}
	
	@Test
	public final void setHeatDamageParametersLegalCase(){
		newSquare.setHeatDamageParameters(100, 50);
		assertEquals(100, newSquare.getHEAT_DAMAGE_LIMIT(), 0);
		assertEquals(50, newSquare.getHEAT_DAMAGE_AMOUNT(), 0);	
	}
	
	//no black box test are worked out to check conditions that violate preconditions
	@Test
	public final void setRustDamageParameters(){
		newSquare.setRustDamageParameters(new BigDecimal("20"),new BigDecimal("27"));
		assertEquals(new BigDecimal("20"),newSquare.getRUST_DAMAGE_LIMIT());
		assertEquals(new BigDecimal("27"),newSquare.getRUST_DAMAGE_AMOUNT());	
	}
	
	@Test
	public final void setHumidityLegalCase(){
		newSquare.setHumidity(new BigDecimal("0.05"));
		assertEquals(new BigDecimal("0.05"), newSquare.getHumidity());
	}
		
	public final void setSlipperyFIRST(){
		Square anotherSquare = new Square(10,new BigDecimal("20"));
		assertEquals(Slippery.SLIPPERY_MATERIAL, anotherSquare.getSlippery());
		}	
		
		@Test
		public final void setSlipperySECOND(){
			Square anotherSquare = new Square(100,new BigDecimal("1"));
			anotherSquare.setMaterial(false);
			anotherSquare.setSlippery();
			assertEquals(Slippery.WATER_SLIPPERYNESS, anotherSquare.getSlippery());
		}
		
		@Test
		public final void setSlipperyTHIRD(){
			Square anotherSquare = new Square(0,new BigDecimal("0.20"));
			anotherSquare.setMaterial(false);
			anotherSquare.setSlippery();
			assertEquals(Slippery.ICE_LAYER, anotherSquare.getSlippery());
		}
		
		
		@Test
		public final void setSlipperyLAST(){
			Square anotherSquare = new Square(10,new BigDecimal("0.05"));
			anotherSquare.setMaterial(false);
			anotherSquare.setSlippery();
			assertEquals(Slippery.NOT_SLIPPERY, anotherSquare.getSlippery());
		}
	
	@Test
	public final void setBorder2(){
		newSquare.setBorder(2);
		assertEquals(true, newSquare.hasBorder(2));
		assertEquals(1, newSquare.hasNumberBorders());
	}
	
	@Test
	public final void setBorder7(){
		newSquare.setBorder(7);
		assertEquals(0, newSquare.hasNumberBorders());
	}
	
	@Test
	public final void setBorderMinus1(){
		newSquare.setBorder(-1);
		assertEquals(0, newSquare.hasNumberBorders());
	}
	
	@Test
	public final void setBorders453(){
		newSquare.setBorder(4,5,3);
		assertEquals(true, newSquare.hasBorder(5));
		assertEquals(3, newSquare.hasNumberBorders());
	}
	
	@Test
	public final void setBorderToMuch(){
		newSquare.setBorder(1,2,3,4,5,6,1);
		assertEquals(6, newSquare.hasNumberBorders());
	}
	
	@Test
	public final void setBordersDoubble(){
		newSquare.setBorder(4,5,3,5);
		assertEquals(true, newSquare.hasBorder(5));
		assertEquals(3, newSquare.hasNumberBorders());
	}
	
	@Test
	public final void deleteBorder4(){
		newSquare.setBorder(4);
		newSquare.deleteBorder(4);
		assertEquals(0, newSquare.hasNumberBorders());
	}
	
	@Test
	public final void deleteBorders(){
		newSquare.setBorder(4,5,6,3);
		newSquare.deleteBorder(4);
		newSquare.deleteBorder(5);
		assertEquals(2, newSquare.hasNumberBorders());
	}
	
	@Test
	public final void calculateNewTemperature85(){
		newSquare.setTemperature(50);
		newSquare.setHumidity(new BigDecimal("0.20"));
		newSquare.setBorder(2);
		mergeSquare.setTemperature(100);
		mergeSquare.setHumidity(new BigDecimal("0.60"));
		mergeSquare.setBorder(2);
		newSquare.mergeWith(mergeSquare, 2);
		assertEquals(85, newSquare.getTemperature(),0);
	}
	
	@Test
	public final void calculateWeights28(){
		BigDecimal[] weights = {new BigDecimal("0.7818"), new BigDecimal("1.2180")};
		assertEquals(weights, newSquare.calculateWeights(new BigDecimal("0.40"),new BigDecimal("0.70")));
	}
	
	@Test (expected = IllegalArgumentException.class)
	public final void calculateWeightsIllegalHumidity(){
		newSquare.calculateWeights(null,new BigDecimal("0.30"));
	}
	
	@Test (expected = IllegalArgumentException.class)
	public final void setIllegalWeightConstant(){
		Square.setWeightConstant(null);
	}
	
	@Test (expected = IllegalArgumentException.class)
	public final void setIllegalWeightConstantBigger(){
		Square.setWeightConstant(new BigDecimal("0.50"));
	}
	
	@Test (expected = IllegalArgumentException.class)
	public final void setIllegalWeightConstantSmaller(){
		Square.setWeightConstant(new BigDecimal("0.00"));
	}
	
	@Test 
	public final void setlegalWeightConstant(){
		Square.setWeightConstant(new BigDecimal("0.30"));
		assertEquals(new BigDecimal("0.30"),newSquare.getWeightConstant());
	}
	
	@Test (expected = IllegalArgumentException.class)
	public final void illegalMerge(){
		newSquare.mergeWith(null,2);
	}
	
	@Test (expected = IllegalArgumentException.class)
	public final void illegalMergeDirection(){
		newSquare.setBorder(2);
		newSquare.mergeWith(mergeSquare,2);
	}
	
	@Test
	public void getInhabitability(){
		newSquare.setHeatDamageParameters(35, 15);
		Square anotherSquare = new Square(150, new BigDecimal("0.5901"));
		assertEquals(-2.86,anotherSquare.getInhabitability(),0);
		
	}
}

